<template>
  <div class="login-wrap">
    <el-form class="login-container">
      <h1 class="title">用户注册</h1>
      <el-form-item>
        <el-input type="text" placeholder="用户名" v-model="userName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input type="password" placeholder="密码" v-model="password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input type="password" placeholder="确认密码" v-model="password1" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input type="text" placeholder="昵称" v-model="nickName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input type="textarea" :rows="2" placeholder="请输入注册码" v-model="content"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button style="float: left;width: 35%" type="primary" @click="register" >注册</el-button>
        <el-button style="float:right;width: 35%" type="primary" @click="login" >返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'page_register',
  data: function () {
    return {
      userName: '',
      password: '',
      password1: '',
      nickName: '',
      content: ''
    }
  },
  methods: {
    register: function () {
      // 先进行初步的检查工作
      if (this.userName === '' || this.userName === null || this.userName === undefined) {
        this.$message.error('用户名不能为空')
      } else if (this.password === '' || this.password === null || this.password === undefined) {
        this.$message.error('密码不能为空')
      } else if (this.password !== this.password1) {
        this.$message.error('两次密码不一致')
      } else if (this.nickName === '' || this.nickName === null || this.nickName === undefined) {
        this.$message.error('昵称不能为空')
      } else if (this.content === '' || this.content === null || this.content === undefined) {
        this.$message.error('注册码不能为空')
      } else {
        // 此时可以给后端发送注册请求
        this.$http({
          method: 'post',
          url: '/admin/register',
          data: {
            adminInformation: {
              userName: this.userName,
              password: this.password,
              nickName: this.nickName
            },
            content: this.content
          }
        }).then((res) => {
          if (res.data.code === '1') {
            this.$message.error(res.data.msg)
          } else if (res.data.code === '0') {
            this.$message.success(res.data.msg)
            // 跳转到登录界面
            this.$router.push('/login')
          }
        })
      }
    },
    login: function () {
      this.$router.push('/login')
    }
  }
}
</script>

<style scoped>
.login-wrap {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding-top: 10%;
  background-image: url();
  /* background-color: #112346; */
  background-repeat: no-repeat;
  background-position: center right;
  background-size: 100%;
}

.login-container {
  border-radius: 10px;
  margin: 0px auto;
  width: 350px;
  padding: 30px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}

.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
</style>

